<?php
/**
 * 法院管理控制器
 */
namespace app\lawcase\controller;

use app\base\BaseController;
use app\lawcase\model\Court as CourtModel;
use app\lawcase\validate\CourtCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Court extends BaseController
{
    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new CourtModel(); // 初始化模型
    }

    /**
     * 法院列表
     * @return \think\Response|\think\response\View
     */
    public function index()
    {
        $param = get_params();
        if (Request::isAjax()) {
            $where = [];
            if (!empty($param['keywords'])) {
                $keywords = trim($param['keywords']);
                $where[] = ['name|address', 'like', '%' . $keywords . '%'];
            }
            $list = $this->model->getList($where, $param);

            // 确保返回的数据格式符合layui table组件的要求
            return json([
                'code' => 0,
                'msg' => '',
                'count' => $list['count'],
                'data' => $list['data']
            ]);
        }
        return view();
    }

    /**
     * 添加/编辑法院
     * @return \think\Response|\think\response\View
     */
    public function add()
    {
        if (Request::isAjax()) {
            $param = get_params();
            try {
                validate(CourtCheck::class)->scene('add')->check($param);
                $result = $this->model->add($param);

                // 统一返回格式
                return json([
                    'code' => $result['code'],
                    'msg' => $result['msg'],
                    'data' => $result['data'] ?? [],
                    'url' => $result['code'] == 0 ? url('lawcase/court/index') : ''
                ]);
            } catch (ValidateException $e) {
                return json(['code' => 1, 'msg' => $e->getError(), 'data' => []]);
            }
        }
        return view();
    }

    /**
     * 编辑法院
     * @return \think\Response|\think\response\View
     */
    public function edit()
    {
        if (Request::isAjax()) {
            $param = Request::param();

            // 严格验证ID参数
            if (empty($param['id']) || !is_numeric($param['id']) || $param['id'] <= 0) {
                return to_assign(1, '无效的法院ID');
            }

            try {
                validate(CourtCheck::class)->scene('edit')->check($param);
                

            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
            $this->model->edit($param);
        }else{

            $id = input('id', 0, 'intval');
            if ($id <= 0) {
                return to_assign(1, '参数错误');
            }

            $detail = $this->model->getById($id);
            if (empty($detail)) {
                return to_assign(1, '法院不存在或已被删除');
            }

            View::assign('detail', $detail);
            return view('add');
        }
    }

    /**
     * 删除法院
     * @return \think\Response
     */
    public function delete()
    {
        $id = get_params("id");
        return $this->model->remove($id);
    }
}